import request from '@/utils/request';
import type { BasicMsg, BasicMsgWithData, CartItem } from '@/types';

// 获取指定用户的购物车
export const getUserCart = (userId: number): Promise<BasicMsgWithData<CartItem[]>> => {
  return request({
    url: `/api/cart/user/${userId}`,
    method: 'get'
  });
};

// 添加商品到购物车
export const addToCart = (data: CartItem): Promise<BasicMsg> => {
  return request({
    url: '/api/cart',
    method: 'post',
    data
  });
};

// 更新购物车商品数量
export const updateCartItem = (id: number, data: CartItem): Promise<BasicMsg> => {
  return request({
    url: `/api/cart/${id}`,
    method: 'put',
    data
  });
};

// 从购物车删除商品
export const deleteCartItem = (id: number): Promise<BasicMsg> => {
  return request({
    url: `/api/cart/${id}`,
    method: 'delete'
  });
};

// 清空用户购物车
export const clearUserCart = (userId: number): Promise<BasicMsg> => {
  return request({
    url: `/api/cart/user/${userId}`,
    method: 'delete'
  });
};

